package com.itheima.reggie.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Cart;
import com.itheima.reggie.service.CartService;
import com.itheima.reggie.service.CategoryService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class CartController{

    @Autowired
    private CartService cartService;
    //添加购物车
    @PostMapping("/cart/add")
    public ResultInfo addCart(@RequestBody Cart cart){
        Cart cartFromDb = cartService.addCart(cart);
        //之所以要返回这个cart对象,是因为前端需要从这个对象中获取number来显示
        return ResultInfo.success(cartFromDb);
    }
    //查询购物车
    @GetMapping("/cart/list")
    public ResultInfo findByCart(){
        List<Cart> cartList = cartService.findByCart();
        return ResultInfo.success(cartList);
    }
    //减少菜品
    @PostMapping("/cart/sub")
    public ResultInfo subNum(@RequestBody Cart cart){
        Cart cartSub =cartService.subNum(cart);
        return ResultInfo.success(cartSub);
    }
    //清空购物车
    @DeleteMapping("/cart/clean")
    public ResultInfo clean(){
        cartService.clean();
        return ResultInfo.success(null);
    }

}
